package com.sdcs.server.daoimpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sdcs.server.model.Student;

@Component
public class StudentDaoImpl {
	@Autowired
	private SessionFactory sessionFactory;
	
	public void delete(String id){
		Session sess = null;
		Transaction tx = null;
		
		try {
			sess = sessionFactory.openSession();
			tx = sess.beginTransaction();
			
			String hql = " delete Student where student_id = ?"; 
			sess.createQuery(hql).setParameter(0, id).executeUpdate();
			
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (sess != null) {
				sess.close();
			}
		} 
	}
	
	public List<Student> getAll(int pageIndex, int pageSize) {
		Session sess = null;
		Transaction tx = null;
		List<Student> list = new ArrayList<Student>();
		
		try {
			sess = sessionFactory.openSession();
			tx = sess.beginTransaction();
			
			String hql = "from Student";
			list = sess.createQuery(hql, Student.class).setFirstResult(pageIndex).setMaxResults(pageSize).getResultList();
			
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (sess != null) {
				sess.close();
			}
		}
		
		return list;
	}
	
	public List<Student> getAll() {
		Session sess = null;
		Transaction tx = null;
		List<Student> list = new ArrayList<Student>();
		
		try {
			sess = sessionFactory.openSession();
			tx = sess.beginTransaction();
			
			String hql = "from Student";
			list = sess.createQuery(hql, Student.class).getResultList();
			
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (sess != null) {
				sess.close();
			}
		}
		
		return list;
	}
	
	public int getTotalCount() {
		Session sess = null;
		Transaction tx = null;
		int rowCount = 0;
		
		try {
			sess = sessionFactory.openSession();
			tx = sess.beginTransaction();
			
			String hql = "from Student";
			rowCount = sess.createQuery(hql).getResultList().size();
			
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (sess != null) {
				sess.close();
			}
		}
		
		return rowCount;
	}
	
	public Student getStudent(String student_name, String password) {
		Session sess = null;
		Transaction tx = null;
		Student student = null;
		try {
			sess = sessionFactory.openSession();
			tx = sess.beginTransaction();
			
			String hql = "from Student where student_name = :student_name and password = :password"; 
			List<Student> list = sess.createQuery(hql, Student.class).setParameter("student_name", student_name).setParameter("password", password).getResultList();
			
			if (!list.isEmpty())
				student = list.get(0);
			
			tx.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (sess != null) {
				sess.close();
			}
		}
		
		return student;
	}
	
	public boolean login(String student_name, String password) {
		Session sess = null;
		Transaction tx = null;
		boolean isLogin = false;
		
		try {
			sess = sessionFactory.openSession();
			tx = sess.beginTransaction();
			
			String hql = "from Student where student_name = :student_name and password = :password"; 
			List<Student> list = sess.createQuery(hql, Student.class).setParameter("student_name", student_name).setParameter("password", password).getResultList();
			
			if (!list.isEmpty())
				isLogin = true;
			
			tx.commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (sess != null) {
				sess.close();
			}
		}
		
		return isLogin;
	}
	
	/**
	 * 判断是否已存在指定的用户
	 * @param userId
	 * @return
	 */
	public boolean isExistsUser(String student_name) {
		Session sess = null;
		Transaction tx = null;
		boolean isExists = false;
		
		try {
			sess = sessionFactory.openSession();
			tx = sess.beginTransaction();
			
			String hql = "from Student where student_name = ?"; 
			List<?> list = sess.createQuery(hql).setParameter(0, student_name).getResultList();
			
			if (!list.isEmpty())
				isExists = true;
			
			tx.commit();
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (sess != null) {
				sess.close();
			}
		}
		
		return isExists;
	}
	
	public boolean insertUser(Student userTb) {
		Session sess = null;
		Transaction tx = null;
		boolean result = false;
		
		try {
			sess = sessionFactory.openSession();
			tx = sess.beginTransaction();
			
			sess.save(userTb);
			
			tx.commit();
			result =  true;
		} catch (Exception e) {
			if (tx != null) {
				tx.rollback();
			}
		} finally {
			if (sess != null) {
				sess.close();
			}
		}
		return result;
	}
}
